package assignment1;

import java.util.LinkedList;


public class Node {

	 private static final int ORDER = 4;        
	 private int numItems;        
	 private Node parent;        
	 private Node childArray[] = new Node[ORDER];        
	 private DataItem itemArray[] = new DataItem[ORDER-1];    
	         
	 public void connectChild(int childNum, Node child){// connect child to this node 
		 childArray[childNum] = child;           
		 if(child != null)              
			 {child.parent = this;}           
		 }    
	     
	 
	 public Node disconnectChild(int childNum){// disconnect child from this node, return it           
		 Node tempNode = childArray[childNum];           
		 childArray[childNum] = null;           
		 return tempNode;           
		 } 
	 
	  public Node getChild(int childNum){ 
		  return childArray[childNum]; 
		  } 
	  
	  public Node getParent(){ 
		  return parent; 
		  } 
	  
	public boolean isLeaf(){ 
		return (childArray[0]==null) ? true : false; 
		}  
	
	 public int getNumItems(){ 
		 return numItems; 
		 }  
	 
	 public DataItem getItem(int index){   // get DataItem at index   
	         
	  return itemArray[index]; 
	 }    
	  public boolean isFull(){ 
		  return (numItems==ORDER-1) ? true : false; 
		  }    
	  public int  findItem(String key) {         // return index of item (within node)        
	                                             
		  for(int j=0; j<ORDER-1; j++)   // if found,                        
			  {                                 // otherwise,              
			  if(itemArray[j] == null)          // return -1                 
				  {break;   }           
			  else if((itemArray[j].getdData()).compareTo(key) == 0)    // itemArray[j].getdData() == key            
				  {
				  	return j;
				  } else{
					  
				  }
			  }           
		  return -1;           
		  }  // end findItem    
			  

	  // -------------------------------------------------------------        
	public int insertItem(DataItem newItem)           
	{    
	       // assumes node is not full           
		numItems++;                          // will add new item          
		String newKey = newItem.getdData();       // key of new item    
	            for(int j=ORDER-2; j>=0; j--)        // start on right,              
	            	{                                 //    examine items              
	            	if(itemArray[j] == null)          // if item null,                 
	            		continue;                      // go left one cell              
	            	else                              // not null,                 
	            		{                              // get its key                 
	            		String itsKey = itemArray[j].getdData();                 
	            		if(newKey.compareTo(itsKey) < 0) // if it's bigger newKey < itsKey    --- lúc đầu là newKey.compareTo(itsKey) == -1          
	            			{itemArray[j+1] = itemArray[j]; }// shift it right                
	            		else                    
	            		{                    
	            			itemArray[j+1] = newItem;   // insert new item                    
	            			return j+1;                 // return index to                    
	            			}                           //    new item                 
	            		}  // end else (not null)              
	            	}  // end for                     // shifted all items,           
	            itemArray[0] = newItem;              // insert new item           
	            return 0;           
	            }  // end insertItem()    
	            		
	           
	  

	public DataItem removeItem()        // remove largest item           
	{           // assumes node not empty           
		DataItem temp = itemArray[numItems-1];  // save item           
		itemArray[numItems-1] = null;           // disconnect it           
		numItems--;                             // one less item           
		return temp;                            // return item           
		}    
	      
	public String displayNode()           // format "/24/56/74/"    // Rewrite this function       
	{      
		String result = "";
		for(int j=0; j<numItems; j++){
			result = result + itemArray[j].displayItem() + "/"; 
		}  // "/56"           
		         // final "/"          
		return result;
	}    
	 
	public LinkedList<Node> ListOfSuccessor()
	{
		LinkedList<Node> arr = new LinkedList<Node>();
		for(int i = 0 ; i< this.childArray.length ; i++){
			arr.add(this.childArray[i]);
		}
		return arr;
	}
	
	public boolean IsSameNode(Node diffnode){
		/*for(int i = 0 ; i < this.numItems;i++){
			for(int j=0; j<diffnode.numItems;j++){
				if(this.itemArray[i] != null){
					if(diffnode.itemArray[j] != null){
						if(this.itemArray[i].getdData().compareTo(diffnode.itemArray[j].getdData()) < 0){
							return false;
						}
						if(this.itemArray[i].getdData().compareTo(diffnode.itemArray[j].getdData()) > 0){
							return false;
						}
					}
				}
				
			}
		}*/
		
		if(this.numItems < diffnode.numItems){return false;}
		if(this.numItems > diffnode.numItems){return false;}
		if(this.numItems == diffnode.numItems){
			for(int i = 0 ; i < this.numItems ; i++){
				if(this.itemArray[i] != null){
					if(diffnode.itemArray[i] != null){
						if(this.itemArray[i].getdData().compareTo(diffnode.itemArray[i].getdData()) < 0){
							return false;
						}
						if(this.itemArray[i].getdData().compareTo(diffnode.itemArray[i].getdData()) > 0){
							return false;
						}
					}
				}
			}
		}
		return true;
	}
	
	public String Name()
	{	
		String name = "|";
		for(int i = 0 ; i< this.numItems;i++){
			name = name + this.itemArray[i].getdData()+"|";
		}
		return name;
		
	}
}
